package com.shenfa.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.Epiboly;
import com.shenfa.bean.FoldOut;
import com.shenfa.bean.FoldoutProd;
import com.shenfa.bean.FoldoutProdB;
import com.shenfa.bean.TreeNode;
import com.shenfa.dao.FoldOutDao;
import com.shenfa.util.ResultObject;
import com.shenfa.util.sequence.SequenceUtil;

/**
 * 插页信息业务逻辑类
 * 
 * @author yanglong
 * 
 * @date 2011-8-23
 *
 */
public class FoldOutService {

	@Autowired
	private FoldOutDao foldOutDao;
	@Autowired
	private SequenceUtil sequenceUtil;
	
	/**
	 * 根据对象条件查询插页列表
	 * @param t	插页对象
	 * @return	插页列表
	 */
	public List<FoldOut> getList(FoldOut t, int start, int limit) {
		return foldOutDao.getList(t, start, limit);
	}
	public int getListCount(FoldOut t) {
		return foldOutDao.getListCount(t);
	}

	
	
	/**
	 * 添加插页
	 * @param t	插页对象
	 * @return	1成功，0失败
	 */
	public int add(FoldOut t) {
		int i=0;
		i=foldOutDao.add(t);
		if(1==i){
			//关联插入，修改所有外包商的个性化数据
			List<Map<String,String>> list=foldOutDao.getFoldoutBySub(t);
			Map<String,String> mm=new HashMap<String,String>();
			mm.put("foldoutid",String.valueOf(t.getFoldoutid()));
			mm.put("pri",String.valueOf(t.getFoldoutpri()));
			for(Map<String,String> m:list){
				mm.put("id",m.get("id"));
				foldOutDao.addFoldOut_to_prod(mm);
			}
		}
		return i;
	}

	/**
	 * 删除插页
	 * @param t	插页对象
	 * @return	1成功，0失败
	 */
	public int del(FoldOut t) {
		int i=foldOutDao.del(t);
		List<Map<String,String>> list=foldOutDao.getFoldoutBySub(t);
		Map<String,String> mm=new HashMap<String,String>();
		mm.put("foldoutid",String.valueOf(t.getFoldoutid()));
		for(Map<String,String> m:list){
			mm.put("id", m.get("id"));
			foldOutDao.delFoldOut_to_prod(mm);
		}
		return i;
	}

	/**
	 * 修改插页
	 * @param t	插页对象
	 * @return	1成功，0失败
	 */
	public int upd(FoldOut t) {
		int i=foldOutDao.upd(t);
		//关联插入，修改所有外包商的个性化数据
		List<Map<String,String>> list=foldOutDao.getFoldoutBySub(t);
		Map<String,String> mm=new HashMap<String,String>();
		mm.put("pri",String.valueOf(t.getFoldoutpri()));
		for(Map<String,String> m:list){
			mm.put("id",m.get("id").toString());
			foldOutDao.updateFoldOut_to_prod(mm);
		}
		return i;
	}
	/**
	 * 根据卡号查询插页详细信息
	 * @param cardid
	 * @return
	 */
	public List<FoldoutProd> listByCard(FoldoutProd foldoutprod,int skipResults,int maxRsults){
		return foldOutDao.listByCard(foldoutprod,skipResults,maxRsults);
	}
	public int queryCountlbc(FoldoutProd foldoutprod){
		return foldOutDao.queryCountlbc(foldoutprod);
	}
	/**
	 * 查询个性化插槽详情
	 * @param cardid
	 * @return
	 */
	public List<FoldoutProdB> listFoldoutDetails(FoldoutProd foldOut){
		return foldOutDao.listFoldoutDetails(foldOut);
	}	
	/**
	 * 查询个性化插页所有账单日
	 * @param cardid
	 * @return
	 */
	public List<FoldoutProd> listFoldoutperiod(int skipResults,int maxRsults){
		return foldOutDao.listFoldoutperiod(skipResults,maxRsults);
	}
	/**
	 * 关联删除插槽信息
	 * @param map
	 * @return
	 */
	public int delFoldOut_to_prod(Map<String,String> map) {
		return foldOutDao.delFoldOut_to_prod(map);
	}
	/**
	 * 关联更新插槽信息
	 * @param map
	 * @return
	 */
	public int updateFoldOut_to_prod(Map<String,String> map) {
		return foldOutDao.updateFoldOut_to_prod(map);
	}
	public  List<TreeNode> listEpiboly()
	{
		return foldOutDao.listEpiboly();
	}
	//获取插页与产品关系表主表ID
	public String getId() {
		return sequenceUtil.getSequence(SequenceUtil.SEQ_FOLDOUT_PROD);
	}
	//修改插页与产品关系表从表
	public int updtsfps(List<FoldoutProdB> fpbs,FoldoutProd fp) 
	{
		String s=fp.getId();
		int a=0;
		for (FoldoutProdB foldoutProdB : fpbs) {
			foldoutProdB.setId(s);
			a=foldOutDao.updtsfp(foldoutProdB);
		}
		return a;
	}
	//删除插页与产品关系表从表
	public int deltsfp(FoldoutProdB fpb)
	{
		return foldOutDao.deltsfp(fpb);
	}
	//根据ID删除个性化模块中  插页的详细信息
	public int delete_tsfps(FoldoutProd foldoutprod)
	{
		return foldOutDao.delete_tsfps(foldoutprod);
	}
	//修改插页与产品关系表主表
	public int edit_tsfps(FoldoutProd fp)
	{
		return foldOutDao.edit_tsfps(fp);
	}
	//增加插页与产品关系表主表
	public int add_tsfps(FoldoutProd fp)
	{
		fp.setId(this.getId());
		return foldOutDao.add_tsfps(fp);
	}
	//验证账期时间的合法性
	public int focedate(FoldoutProd fp)
	{
		return foldOutDao.focedate(fp);
	}
	//验证插槽合法性
	public int focefoldoutid(FoldOut t)
	{
		return foldOutDao.focefoldoutid(t);
	}
    //在新增功能模块中循环添加插页和产品的
	public int addfoldpb(List<FoldoutProdB> fpbs,FoldoutProd fp)
	{
		int a=0;
		String id=fp.getId();
		for (FoldoutProdB foldoutProdB : fpbs) {
			foldoutProdB.setId(id);
			a=foldOutDao.addfoldpb(foldoutProdB);
		}
		return a;
	}
}
